package com.sun.filter;

import com.sun.pojo.User;
import com.sun.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Sun
 * @Date 2020/4/25 20:11
 * @Version 1.0
 */
public class SysFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //登陆拦截的过滤器
        HttpServletRequest req = (HttpServletRequest)request;//向下转型
        HttpServletResponse resp = (HttpServletResponse)response;

        //从session中获取user对象
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);

        if(user==null){//已经被移除、注销或者未登录
            resp.sendRedirect(req.getContextPath()+"/syserror.jsp");
        }else {
            chain.doFilter(request, response);
        }

    }

    public void destroy() {

    }
}
